<?php

namespace CloudFlare;

use think\Log;


class CloudFlareApiClient
{
    private $api;

    function __construct()
    {
        $this->api = new CloudFlareApi();
    }

    /**
     * 根据ip查询地址信息
     * @param $ip
     * @return mixed
     */
    public function getAccounts()
    {
        $apiPath = CloudFlareApiConstent::API_LIST_ACCOUNTS;
        $data = [

        ];
        Log::info('CloudFlareApi getAccounts,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }

    /**
     * 查询账号信息
     * @return mixed
     */
    public function getAccountDetail()
    {
        $apiPath = CloudFlareApiConstent::API_ACCOUNTS_DETAIL;
        $data = [

        ];
        Log::info('CloudFlareApi getAccountDetail,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }

    /**
     * 复制视频
     * @param $url
     * @param $name
     * @return mixed
     */
    public function postUploadVideo($url, $name)
    {
        $apiPath = CloudFlareApiConstent::API_STREAM_COPY;
        $meta = [
            'name' => $name,
        ];
        $data = [
            'url' => $url,
            'meta' => $meta,
        ];
        Log::info('CloudFlareApi postUploadVideo,params:' . json_encode($data));
        return $this->api->post($apiPath, $data);
    }

    /**
     * 获取库用量
     * @return mixed
     */
    public function getStorageUseage()
    {
        $apiPath = CloudFlareApiConstent::API_STREAM_STORAGE_USAGE;
        $data = [

        ];
        Log::info('CloudFlareApi getStorageUseage,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 获取视频信息
     * @param $uid
     * @return mixed
     */
    public function getVideoDetail($uid)
    {
        $apiPath = str_replace('{identifier}', $uid, CloudFlareApiConstent::API_STREAM_VIDEO_DETAILS);
        $data = [
        ];
        Log::info('CloudFlareApi getVideoDetail,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }

    /**
     * 获取MP4下载地址
     * @param $uid
     * @return mixed
     */
    public function getVideoDownloads($uid)
    {
        $apiPath = str_replace('{identifier}', $uid, CloudFlareApiConstent::API_STREAM_VIDEO_DOWNLOADS);
        $data = [
        ];
        Log::info('CloudFlareApi getVideoDownloads,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 开启MP4下载地址
     * @param $uid
     * @return mixed
     */
    public function postVideoDownloads($uid)
    {
        $apiPath = str_replace('{identifier}', $uid, CloudFlareApiConstent::API_STREAM_VIDEO_DOWNLOADS);
        $data = [
        ];
        Log::info('CloudFlareApi postVideoDownloads,params:' . json_encode($data));
        return $this->api->post($apiPath, $data);
    }

    /**
     * 设置webhook
     * @param $url
     * @return mixed
     */
    public function putWebHook($url)
    {
        $apiPath = CloudFlareApiConstent::API_STREAM_WEBHOOK;
        $data = [
            'notificationUrl' => $url
        ];
        Log::info('CloudFlareApi putWebHook,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }


    /**
     * 设置webhook
     * @param $url
     * @return mixed
     */
    public function postDirectUpload($name)
    {
        $apiPath = CloudFlareApiConstent::API_STREAM_DIRECT_UPLOAD;
        $meta = [
            'name' => $name,
        ];
        $data = [
            'meta' => $meta,
            'maxDurationSeconds' => 21600
        ];
        Log::info('CloudFlareApi postDirectUoload,params:' . json_encode($data));
        return $this->api->post($apiPath, $data);
    }

    /**
     * 上传本地视频
     * @param $url
     * @param $name
     * @return mixed
     */
    public function postDirectUploadVideo($url, $filepath, $title)
    {
        $data = [
            'file' => new \CURLFile($filepath, '', $title),
        ];
        Log::info('CloudFlareApi postDirectUploadVideo,params:' . json_encode($data));
        return $this->api->postFile($url, $data);
    }


    /**
     * 视频列表
     * @return mixed
     */
    public function getVideoList($startAt = null, $endAt = null)
    {
        $apiPath = CloudFlareApiConstent::API_STREAM_VIDEO_LIST;
        $data = [
            'include_counts' => true,
            'asc' => true
        ];

        if (!empty($startAt)) {
            $data['start'] = $startAt;
        }

        if (!empty($endAt)) {
            $data['end'] = $endAt;
        }

        Log::info('CloudFlareApi getVideoList,params:' . json_encode($data));
        return $this->api->get($apiPath, $data);
    }

    /**
     * 删除视频信息
     * @param $uid
     * @return mixed
     */
    public function deleteVideo($uid)
    {
        $apiPath = str_replace('{identifier}', $uid, CloudFlareApiConstent::API_STREAM_VIDEO_DELETE);
        $data = [
        ];
        Log::info('CloudFlareApi deleteVideo,params:' . json_encode($data));
        return $this->api->delete($apiPath, $data);
    }
}